package com.example.spring.common;

import com.example.spring.entity.Redis;
import javafx.scene.effect.SepiaTone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by admin on 6/15/17.
 */


@Configuration
@ConditionalOnClass({JedisCluster.class})
@EnableConfigurationProperties({Redis.class})
public class JedisClusterConfig {

    @Autowired
    Redis redis;

    @Bean
    public JedisCluster getJedisCluster(){

        String[] serverArray = redis.getClusterNodes().split(",");
        Set<HostAndPort> nodes = new HashSet<HostAndPort>();

        for(String server : serverArray){
            String[] ipPorts = server.split(":");
            nodes.add(new HostAndPort(ipPorts[0].trim(),Integer.valueOf(ipPorts[1].trim())));
        }

        return new JedisCluster(nodes,redis.getCommandTimeout());
    }

}
